home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / igamma.pro < prev    next >
Encoding:
Text File  |  1997-07-08  |  3.6 KB  |  117 lines

  1. ;$Id: igamma.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       IGAMMA
  8. ;
  9. ; PURPOSE:
  10. ;       This function computes the incomplete gamma function, Px(a).
  11. ;
  12. ; CATEGORY:
  13. ;       Special Functions.
  14. ;
  15. ; CALLING SEQUENCE:
  16. ;       Result = Igamma(a, x)
  17. ;
  18. ; INPUTS:
  19. ;       A:    A positive scalar of type integer, float or double that
  20. ;             specifies the parametric exponent of the integrand.
  21. ;
  22. ;       X:    A positive scalar of type integer, float or double that 
  23. ;             specifies the upper limit of integration.
  24. ;
  25. ; KEYWORD PARAMETERS:
  26. ;       METHOD:  Use this keyword to specify a named variable which returns
  27. ;                the method used to compute the incomplete gamma function.
  28. ;                A value of 0 indicates that a power series representation
  29. ;                was used. A value of 1 indicates that a continued fractions
  30. ;                method was used.
  31. ;
  32. ; EXAMPLE:
  33. ;       Compute the incomplete gamma function for the corresponding elements
  34. ;       of A and X.
  35. ;       Define the parametric exponents.
  36. ;         A = [0.10, 0.50, 1.00, 1.10, 6.00, 26.00]
  37. ;       Define the the upper limits of integration.
  38. ;         X = [0.0316228, 0.0707107, 5.00000, 1.04881, 2.44949, 25.4951]
  39. ;       Allocate an array to store the results.
  40. ;         result = fltarr(n_elements(A))
  41. ;       Compute the incomplete gamma functions.
  42. ;         for k = 0, n_elements(A)-1 do $
  43. ;           result(k) = Igamma(A(k), X(k))
  44. ;       The result should be:
  45. ;         [0.742026, 0.293128, 0.993262, 0.607646, 0.0387318, 0.486387]
  46. ;
  47. ; PROCEDURE:
  48. ;       IGAMMA computes the incomplete gamma function, Px(a), using either
  49. ;       a power series representation or a continued fractions method. If X
  50. ;       is less than or equal to A+1, a power series representation is used.
  51. ;       If X is greater than A+1, a continued fractions method is used. 
  52. ; REFERENCE:
  53. ;       Numerical Recipes, The Art of Scientific Computing (Second Edition)
  54. ;       Cambridge University Press
  55. ;       ISBN 0-521-43108-5
  56. ;
  57. ; MODIFICATION HISTORY:
  58. ;       Written by:  GGS, RSI, September 1994
  59. ;                    IGAMMA is based on the routines: gser.c, gcf.c, and  
  60. ;                    gammln.c described in section 6.2 of Numerical Recipes,
  61. ;                    The Art of Scientific Computing (Second Edition), and is
  62. ;                    used by permission.
  63. ;       Modified:    GGS, RSI, January 1996
  64. ;                    Corrected the case of IGAMMA(a, 0) for a > 0.
  65. ;-
  66.  
  67. function igamma, a, x, itmax = itmax, method = method
  68.  
  69.   on_error, 2
  70.  
  71.   if a le 0 or x lt 0 then $
  72.     message, 'a and x must be positive scalars.'
  73.  
  74.   if x eq 0.0 then return, 0.0
  75.  
  76.   eps = 3.0e-7
  77.   fpmin = 1.0e-30
  78.   if keyword_set(itmax) eq 0 then itmax = 100
  79.  
  80.   if x le (a + 1) then begin ;Series Representation.
  81.     method = 0
  82.     ap = a
  83.     sum = 1.0 / a
  84.     del = sum
  85.     for k = 1, itmax do begin
  86.       ap = ap + 1.0
  87.       del = del * x / ap
  88.       sum = sum + del
  89.       if abs(del) lt abs(sum)*eps then return, $
  90.         sum * exp(-x + a * alog(x) - lngamma(a))
  91.     endfor 
  92.   endif else begin ;Continued Fractions.
  93.     method = 1
  94.     b = x + 1.0 - a
  95.     c = 1.0 / fpmin
  96.     d = 1.0 / b
  97.     h = d
  98.     for k = 1, itmax do begin
  99.       an = -k * (k - a)
  100.       b = b + 2
  101.       d = an * d + b
  102.       if abs(d) lt fpmin then d = fpmin
  103.       c = b + an / c
  104.       if abs(c) lt fpmin then c = fpmin
  105.       d = 1.0 / d
  106.       del = d * c
  107.       h = h * del
  108.       if abs(del - 1) lt eps then return, $
  109.         1 - (exp(-x + a * alog(x) - lngamma(a)) * h)
  110.     endfor
  111.   endelse
  112.  
  113.   message, 'Failed to converge within given parameters.'
  114.  
  115. end
  116.